package com.pureut.equipment.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.equipment.domain.dto.InspectionItemPlanDto;
import com.pureut.equipment.domain.dto.PatrolItemPlanDto;
import com.pureut.equipment.domain.vo.InspectionItemPlanVo;
import com.pureut.equipment.domain.vo.PatrolItemPlanVo;
import com.pureut.equipment.service.PatrolItemPlanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/***
 *
 * Author:巡检计划控制层
 * Date:2023/04/26 10:45
 * @DESC
 */
@RestController
@RequestMapping("/patrol-item-plan")
public class PatrolItemPlanController extends BaseController {

    /**
     * 巡检计划service
     */
    @Resource
    PatrolItemPlanService patrolItemPlanService;

    /**
     * 巡检计划列表
     *
     * @param patrolItemPlanVo
     * @return
     */
    @RequiresPermissions("device:inspection:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(PatrolItemPlanVo patrolItemPlanVo) {
        startPage();
        List<PatrolItemPlanDto> list = patrolItemPlanService.getList(patrolItemPlanVo);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @RequiresPermissions("device:inspection:project:add")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody PatrolItemPlanVo patrolItemPlanVo) throws Exception {
        return toAjax(patrolItemPlanService.add(patrolItemPlanVo));
    }

    /**
     * 删除
     */
    @RequiresPermissions("device:inspection:project:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult deletePatrolItemPlan(@PathVariable("id") Long id) {
        return toAjax(patrolItemPlanService.deletePatrolItemPlan(id));
    }

    /**
     * 修改
     */
    @RequiresPermissions("device:inspection:project:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePatrolItemPlan")
    public AjaxResult updatePatrolItemPlan(@RequestBody PatrolItemPlanVo patrolItemPlanVo) {
        return toAjax(patrolItemPlanService.updatePatrolItemPlan(patrolItemPlanVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("device:inspection:project:query")
    @GetMapping("/getView/{id}")
    public AjaxResult getView(@PathVariable("id") Long id) {
        return AjaxResult.success(patrolItemPlanService.getView(id));
    }

    /**
     * 启用
     */
    @RequiresPermissions("device:inspection:project:enable")
    @GetMapping("/enable/{id}")
    public AjaxResult enable(@PathVariable("id") Long id) throws Exception {
        return toAjax(patrolItemPlanService.enable(id));
    }

    /**
     * 停用
     */
    @RequiresPermissions("device:inspection:plan:stop")
    @GetMapping("/stop/{id}")
    public AjaxResult stop(@PathVariable("id") Long id) {
        return toAjax(patrolItemPlanService.stop(id));
    }

    /**
     * 根据巡检计划开始时间和结束时间定时修改计划状态(定时任务调用)
     */
    @PostMapping("/updatePatrolStatusByTime")
    public AjaxResult updatePatrolStatusByTime() throws ParseException {
        return toAjax(patrolItemPlanService.updatePatrolStatusByTime());
    }
}
